package com.taobao.basic.service.impl;

import com.taobao.basic.mapper.BaseMapper;
import com.taobao.basic.query.BaseQuery;
import com.taobao.basic.service.IBaseService;
import com.taobao.basic.util.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class BaseServiceImpl<T> implements IBaseService<T> {

    @Autowired
    private BaseMapper<T> baseMapper;


    /**
     * 加载所有数据
     * @return
     */
    @Override
    public List<T> loadAll() {

        return baseMapper.loadAll();
    }

    /**
     * 加载单个数据
     * @return
     */
    @Override
    public T loadById(Long id) {

        return baseMapper.loadById(id);
    }

    /**
     * 新增数据
     * @param t
     */
    @Transactional
    @Override
    public void save(T t) {
        baseMapper.save(t);
    }

    /**
     * 删除数据
     * @param id
     */
    @Transactional
    @Override
    public void remove(Long id) {
        baseMapper.remove(id);
    }

    /**
     * 修改数据
     * @param t
     */
    @Transactional
    @Override
    public void update(T t) {
        baseMapper.update(t);
    }

    /**
     * 分页查询数据
     * @param query
     * @return
     */
    // 分页信息
    public PageList<T> pageQuery(BaseQuery query){

        Integer count = baseMapper.queryCount(query);

        List<T> rows = baseMapper.queryData(query);

        // 分页数据
        return new PageList<>(count,rows);
    }
    /**
     * 删除所有操作
     * @param ids
     */
    @Transactional
    @Override
    public void patchDel(List<Long> ids) {
        baseMapper.patchDel(ids);
    }
}
